package com.vegetable.modules.controller.finance;

import com.alibaba.fastjson.JSON;
import com.vegetable.common.annotation.SysLog;
import com.vegetable.common.utils.PageUtils;
import com.vegetable.common.utils.R;
import com.vegetable.modules.controller.AbstractController;
import com.vegetable.modules.entity.finance.PaymentPurchase;
import com.vegetable.modules.entity.finance.po.PaymentPurchaseAddPO;
import com.vegetable.modules.service.finance.IPaymentPurchaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 采购付款 前端控制器
 * </p>
 *
 * @author wangruijie
 * @since 2024-04-15
 */
@Slf4j
@RestController
@RequestMapping("/payment/purchase")
@AllArgsConstructor
@Api(value = "采购付款", tags = "采购付款")
public class PaymentPurchaseController extends AbstractController {

    private IPaymentPurchaseService paymentService;
    /**
     * 采购付款列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "条件查询采购付款列表", notes = "条件查询采购付款列表")
    public R list(@RequestParam Map<String, Object> params){
        // 状态0采购付款，1代办付款
        params.put("orderType", 0);
        log.info("条件查询采购付款列表{}", JSON.toJSONString(params));
        PageUtils page = paymentService.queryPage(params);
        return R.data( page);
    }

    /**
     * 新增采购付款
     */
    @SysLog(value = "新增采购付款")
    @PostMapping("/savePayment")
    @ApiOperation(value = "新增采购付款", notes = "新增采购付款")
    public R savePayment(@RequestBody PaymentPurchaseAddPO payment) throws Exception {
        log.info("新增采购付款：{}", JSON.toJSONString(payment));
        payment.setCreateUserId(getUserId());
        payment.setOrderType(0);
        return paymentService.savePayment(payment);
    }

    /**
     * 详情采购付款
     */
    @SysLog(value = "详情采购付款")
    @GetMapping("/detailPurchase")
    @ApiOperation(value = "根据id查询详情采购付款", notes = "根据id查询详情采购付款")
    public R detailPurchase(Long id){
        if (id == null || id == -1){
            return R.fail("订单主键不能为空");
        }
        log.info("根据id查询详情采购付款：{}", JSON.toJSONString(id));
        return paymentService.detailPurchase(id);
    }

    /**
     * 收款确定
     */
    @SysLog(value = "收款确定")
    @PostMapping("/completePayment")
    @ApiOperation(value = "收款确定", notes = "收款确定")
    public R completePayment(@RequestBody PaymentPurchaseAddPO payment){
        log.info("收款确定：{}", JSON.toJSONString(payment));
        if (payment.getId()==null){
            return R.fail("订单id主键不能为空");
        }
        payment.setOrderType(0);
        payment.setState(1); // 设置为实际已付状态
        try {
            return paymentService.savePayment(payment);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

    /**
     * 删除采购付款
     */
    @SysLog(value = "删除采购付款")
    @GetMapping("/deletePayment")
    @ApiOperation(value = "根据id删除采购付款", notes = "根据id删除采购付款")
    public R deletePayment(Long id){
        log.info("删除采购付款：{}", JSON.toJSONString(id));
        try {
            return paymentService.deletePayment(id);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

}